package com.pointtown.vcrss.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class RSSTabeleDAO {

	private RSSOpenHelper helper;
	private String TAG = "RSSTabeleDAO";
	public RSSTabeleDAO(Context context) {
		helper = new RSSOpenHelper(context);
	}

	public Entry getEntryById(String id) {

		Entry entry = null;
		SQLiteDatabase database = null;
		Cursor cursor = null;

		try{

			database = helper.getReadableDatabase();
			cursor = database.query(RSSOpenHelper.RSS_TABLE, null, "_id ="
					+ id, null, null, null, null);
			if (cursor != null) {
				cursor.moveToFirst();
				entry = cnvertEntry(cursor);
			}
		}catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}finally{
			if(cursor != null){
				cursor.close();
			}
			if(database != null){
				database.close();
			}
		}
		return entry;
	}

	public ArrayList<Entry> getAllEntries(){
		ArrayList<Entry> entryList = null;

		SQLiteDatabase database = null;
		Cursor cursor = null;
		
		try{
			
			database = helper.getReadableDatabase();
	
			cursor = database.query(RSSOpenHelper.RSS_TABLE, null, null, null,
					null, null, null);
			if (cursor != null) {
	
				entryList = new ArrayList<Entry>();
				while (cursor.moveToNext()) {
					Entry entry = cnvertEntry(cursor);
					entryList.add(entry);
				}
			}
		}catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}finally{
			if(cursor != null){
				cursor.close();
			}
			if(database != null){
				database.close();
			}
		}
		return entryList;
	}

	private Entry cnvertEntry(Cursor cursor) {
		Entry entry = new Entry();
		entry.setId(cursor.getString(cursor.getColumnIndex(RSSOpenHelper.ID)));
		entry.setTitle(cursor.getString(cursor.getColumnIndex(RSSOpenHelper.TITLE)));
		entry.setLink(cursor.getString(cursor.getColumnIndex(RSSOpenHelper.LINK)));
		entry.setPubDate(cursor.getString(cursor.getColumnIndex(RSSOpenHelper.PUB_DATE)));
		entry.setCategory(cursor.getString(cursor.getColumnIndex(RSSOpenHelper.CATEGORY)));
		entry.setDescription(cursor.getString(cursor.getColumnIndex(RSSOpenHelper.DESCRIPTION)));
		entry.setRegistDate(cursor.getString(cursor.getColumnIndex(RSSOpenHelper.REGIST_DATE)));
		entry.setUpdateDate(cursor.getString(cursor.getColumnIndex(RSSOpenHelper.UPDATE_DATE)));
		return entry;
	}

	/**
	 * 
	 * @param entreis
	 * @throws Exception 
	 */
	public long insertEntries(ArrayList<Entry> entreis) {
		
//		Log.v("", " insert: " + new Throwable().getStackTrace()[1]);

		SQLiteDatabase database = null;
		long cnt = 0;
		
		try{
			database = helper.getWritableDatabase();
	
			for (Entry entry : entreis) {
				ContentValues values = new ContentValues();
	
				values.put(RSSOpenHelper.TITLE, entry.getTitle());
				values.put(RSSOpenHelper.LINK, entry.getLink());
				values.put(RSSOpenHelper.PUB_DATE, entry.getPubDate());
				values.put(RSSOpenHelper.DESCRIPTION, entry.getDescription());
				values.put(RSSOpenHelper.CATEGORY, entry.getCategory());
				values.put(RSSOpenHelper.UPDATE_DATE, entry.getUpdateDate());
				values.put(RSSOpenHelper.REGIST_DATE, entry.getRegistDate());
	
				cnt = database.insert(RSSOpenHelper.RSS_TABLE, null, values);
			}
		}catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}finally{
			if(database != null){
				database.close();
			}
		}
		
		return cnt;
	}
	
	public int deleteEntryById(String id) {

		return delete(id);
	}
	
	public int delteAllEntries() {
		
		return delete(null);
	}
	
	public int delete(String id) {
		SQLiteDatabase database = null;
		int cnt = 0;
		
		try{
			database = helper.getWritableDatabase();
			
			if(id == null || id.equals("")){
				cnt = database.delete(RSSOpenHelper.RSS_TABLE, null, null);				
			}else{
				cnt = database.delete(RSSOpenHelper.RSS_TABLE, "_id =", null);
			}
			
		}catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}finally{
			if(database != null){
				database.close();
			}
		}
		
		return cnt;
	}
	
	public int getEntryCount() {
		int count = 0;
		
		SQLiteDatabase database = null;
		Cursor cursor = null;
		try{
			database = helper.getReadableDatabase();
			cursor = database.query(RSSOpenHelper.RSS_TABLE, new String[]{"count(*)"}, null, null, null, null, null);
			if (cursor != null) {
				cursor.moveToFirst();
				count = cursor.getInt(0);
			}
		}catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}finally{
			if(database != null){
				database.close();
			}
		}
		return count;
	}
	
	
}
